package com.example.gateway.middle.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author wangjinlong
 * @date 2022/2/17 9:51
 * 也可以在bootstrap.yaml中配置, 优先使用配置文件中的
 * spring:
 *   cloud:
 *     sentinel:
 *       scg:
 *         fallback:
 *           mode: response
 *           response-status: 201
 *           response-body: '{"code": 201,"message": "请求失败，稍后重试！"}'
 */
public class MyBlockRequestHandler implements BlockRequestHandler {

    @Override
    public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
        // 返回http状态码为201
        return ServerResponse.status(201)
                .contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE))
                .bodyValue("{\"code\": 201,\"message\": \"请求失败，稍后重试!!!!!!!!!!!!!!!!!\"}");
    }
}
